//
//  CYBettingTitleView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/17.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYBettingTitleView.h"
#define left_right_width 12 //左右相距
#define btn_height 29 //按钮高度
@interface CYBettingTitleView(){
    bettingTitleBlock _myblock;
    NSUInteger _firstIndex;  //一级玩法坐标
    NSUInteger _secondIndex; //二级玩法坐标
    NSUInteger _first_row;     //一级选法  行数
    NSUInteger _second_row;   //二级玩法 行数
}
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) CYLabel *subTitleLBL;
@property (nonatomic, strong) CYView *titleLBL_leftLine;//一级玩法标题左划线
@property (nonatomic, strong) CYView *titleLBL_rightLine;//一级玩法标题右划线
@property (nonatomic, strong) CYView *subTitleLBL_leftLine;//二级玩法标题左划线
@property (nonatomic, strong) CYView *subTitleLBL_rightLine;//二级玩法标题右划线
@property (nonatomic, copy) NSString *firstTitle;//一级玩法名称
@property (nonatomic, copy) NSString *secondTitle;//二级玩法名称

@end
@implementation CYBettingTitleView
- (instancetype)initWithColor:(UIColor *)color withData:(NSArray *)array block:(bettingTitleBlock)block {
    self = [super initWithColor:color];
    if (self) {
        _myblock = block;
        _firstIndex = 0;//一级玩法坐标
        _secondIndex = 0;//二级玩法坐标
        self.firstTitle = [[array[_firstIndex] allKeys] firstObject];//一级玩法名称
        self.betWay = array;//一级玩法数组
        NSArray *subArr = (NSArray *)[array[0] valueForKey:[[array[0] allKeys]firstObject]];
        self.secondTitle = subArr[0];//二级玩法名称
        self.subBetWay = (NSArray *)[array[0] valueForKey:[[array[0] allKeys]firstObject]];//二级玩法数组
    }
    return self;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:nil size:AL(PT(26)) textcolor:ColorHex(0x888888)];
        _titleLBL.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).with.offset(left_right_width);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYLabel *)subTitleLBL{
    if (!_subTitleLBL) {
        _subTitleLBL = [[CYLabel alloc]initWithText:nil size:AL(PT(26)) textcolor:ColorHex(0x888888)];
        [self addSubview:_subTitleLBL];
        
        [_subTitleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(left_right_width + (btn_height + left_right_width) * _first_row);
            make.size.mas_equalTo(_subTitleLBL.bounds.size);
        }];
    }
    return _subTitleLBL;
}

- (CYView *)titleLBL_leftLine{
    if (!_titleLBL_leftLine) {
        _titleLBL_leftLine = [[CYView alloc]initWithColor:TEXT_GRAY_COLOR];
        [self addSubview:_titleLBL_leftLine];
        
        [_titleLBL_leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_titleLBL);
            make.right.equalTo(_titleLBL.mas_left).with.offset(- left_right_width);
            make.size.mas_equalTo (CGSizeMake((SCREEN_WIDTH - _titleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
        }];
    }
    return _titleLBL_leftLine;
}

- (CYView *)titleLBL_rightLine{
    if (!_titleLBL_rightLine) {
        _titleLBL_rightLine = [[CYView alloc]initWithColor:TEXT_GRAY_COLOR];
        [self addSubview:_titleLBL_rightLine];
        
        [_titleLBL_rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_titleLBL);
            make.left.equalTo(_titleLBL.mas_right).with.offset(left_right_width);
            make.size.mas_equalTo (CGSizeMake((SCREEN_WIDTH - _titleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
        }];
    }
    return _titleLBL_rightLine;
}

- (CYView *)subTitleLBL_leftLine{
    if (!_subTitleLBL_leftLine) {
        _subTitleLBL_leftLine = [[CYView alloc]initWithColor:TEXT_GRAY_COLOR];
        [self addSubview:_subTitleLBL_leftLine];
        
        [_subTitleLBL_leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_subTitleLBL);
            make.right.equalTo(_subTitleLBL.mas_left).with.offset(- left_right_width);
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - _subTitleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
        }];
    }
    return _subTitleLBL_leftLine;
}

- (CYView *)subTitleLBL_rightLine{
    if (!_subTitleLBL_rightLine) {
        _subTitleLBL_rightLine = [[CYView alloc]initWithColor:TEXT_GRAY_COLOR];
        [self addSubview:_subTitleLBL_rightLine];
        
        [_subTitleLBL_rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_subTitleLBL);
            make.left.equalTo(_subTitleLBL.mas_right).with.offset(left_right_width);
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - _subTitleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
        }];
    }
    return _subTitleLBL_rightLine;
}

//一级玩法名称
- (void)setFirstTitle:(NSString *)firstTitle{
    if (_firstTitle != firstTitle)
        _firstTitle = firstTitle;
    self.titleLBL.text = _firstTitle;
    self.titleLBL_leftLine.hidden = self.titleLBL_rightLine.hidden = NO;
    [_titleLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_titleLBL.bounds.size);
    }];
    
    [_titleLBL_leftLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo (CGSizeMake((SCREEN_WIDTH - _titleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
    }];
    
    [_titleLBL_rightLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo (CGSizeMake((SCREEN_WIDTH - _titleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
    }];
    
}

//二级玩法名称
- (void)setSecondTitle:(NSString *)secondTitle{
    if (_secondTitle != secondTitle)
        _secondTitle = secondTitle;
    self.subTitleLBL.text = _secondTitle;
    self.subTitleLBL_leftLine.hidden = self.subTitleLBL_rightLine.hidden = NO;
    
    [_subTitleLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_subTitleLBL.bounds.size);
    }];
    
    [_subTitleLBL_leftLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - _subTitleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
    }];
    
    [_subTitleLBL_rightLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - _subTitleLBL.bounds.size.width - left_right_width * 4) / 2, 0.5));
    }];
}

//一级玩法数组
- (void)setBetWay:(NSArray *)betWay{
    if (_betWay != betWay)
        _betWay = betWay;
    _first_row = (!(betWay.count%4))?betWay.count / 4:betWay.count / 4 + 1; //一级玩法 行数
    [self setupBetWayBtn];
}

//二级玩法数组
- (void)setSubBetWay:(NSArray *)subBetWay{
    if (_subBetWay != subBetWay)
        _subBetWay = subBetWay;
    _second_row = (!(subBetWay.count%4))?subBetWay.count / 4:subBetWay.count / 4 + 1;      //二级玩法 行数
    [self setupBounds];
    [self clear];
    [self setupSubBetWayBtn];
    
}

//设置bounds
- (void)setupBounds{
    CGFloat textHeight = ceil([@"前二" sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:AL(PT(26))]}].height); // 文本高度
    self.bounds = CGRectMake(0.0, 0.0,SCREEN_WIDTH, (textHeight + left_right_width * 2) * 2 + (btn_height + left_right_width) * (_first_row + _second_row));
}

- (void)clear{
    [self ergodicSubView:^(CYView *view) {
        if ([view isKindOfClass:[CYButton class]] && view.tag >= 300){
            [view removeFromSuperview];
            view = nil;
        }
    }];
}

//
- (void)setupBetWayBtn{
    
    CGFloat width = (SCREEN_WIDTH - 5 * left_right_width) / 4.0;   //按钮的宽度
    for (int i = 0; i < _betWay.count; i ++) {
        CYButton *betWayBtn = [[CYButton alloc]initWithTitle:[[_betWay[i] allKeys] firstObject] titleColor:ColorHex(0x888888) backgroundColor:[UIColor clearColor] cornerRadius:AL(5)];
        [betWayBtn addTarget:self action:@selector(actionForBetWayBtn:) forControlEvents:UIControlEventTouchUpInside];
        betWayBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(28))];
        [betWayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [betWayBtn setBackgroundColor:[UIColor clearColor] forstate:UIControlStateNormal];
        [betWayBtn setBackgroundColor:THEME_COLOR forstate:UIControlStateSelected];
        betWayBtn.layer.borderColor = (i == _firstIndex)?[UIColor clearColor].CGColor:ColorHex(0x888888).CGColor;
        betWayBtn.layer.borderWidth = 0.5;
        betWayBtn.selected = (i == _firstIndex)?YES:NO;
        betWayBtn.tag = 200 + i;
        [self addSubview:betWayBtn];
        
        NSUInteger column = i%4;  //列
        NSUInteger row = i / 4;
        [betWayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(left_right_width + (btn_height + left_right_width) * row);
            make.left.equalTo(self).with.offset(AL(PT(24)) + (width + left_right_width) * column);
            make.size.mas_equalTo(CGSizeMake(width, btn_height));
        }];
    }
}

- (void)setupSubBetWayBtn{
    
    CGFloat offsetY = AL(PT(24));
    CGFloat width = (SCREEN_WIDTH - 5 * left_right_width) / 4.0;   //按钮的宽度
    
    for (int i = 0; i < _subBetWay.count; i ++) {
        CYButton *sub_betWayBtn = [[CYButton alloc]initWithTitle:_subBetWay[i] titleColor:ColorHex(0x888888) backgroundColor:[UIColor clearColor] cornerRadius:AL(5)];
        [sub_betWayBtn addTarget:self action:@selector(actionForSubBetWayBtn:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat text_width = [_subBetWay[i] sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:12]}].width; // 文本高度
        sub_betWayBtn.titleLabel.font = (width < text_width)?[UIFont systemFontOfSize:AL(PT(20))]:[UIFont systemFontOfSize:12]; //文字自适应按钮大小;
        [sub_betWayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [sub_betWayBtn setBackgroundColor:[UIColor clearColor] forstate:UIControlStateNormal];
        [sub_betWayBtn setBackgroundColor:THEME_COLOR forstate:UIControlStateSelected];
        sub_betWayBtn.layer.borderColor = (i == _secondIndex)?[UIColor clearColor].CGColor:ColorHex(0x888888).CGColor;
        sub_betWayBtn.layer.borderWidth  = 0.5;
        sub_betWayBtn.selected = (i == _secondIndex)?YES:NO;
        sub_betWayBtn.tag = 300 + i;
        [self addSubview:sub_betWayBtn];
        
        NSUInteger column = (i < 4)?i:(i - (i / 4) * 4);  //列
        NSUInteger row = i / 4;
        [sub_betWayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_subTitleLBL.mas_bottom).with.offset(offsetY + (offsetY + btn_height) * row);
            make.left.equalTo(self).with.offset(offsetY + (width+ offsetY) * column);
            make.size.mas_equalTo(CGSizeMake(width, btn_height));
        }];
    }
    
}

//一级玩法，选择
- (void)actionForBetWayBtn:(CYButton *)sender{
    [self actionForBtn:sender tag:200];
    _secondIndex = 1000;//二级坐标归零
    _secondTitle = nil;//二级玩法名称清零
    _firstIndex = sender.tag - 200;
    self.subBetWay = (NSArray *)[_betWay[_firstIndex] valueForKey:[[_betWay[_firstIndex] allKeys]firstObject]];//获取相应的二级玩法数组
    self.firstTitle = [sender titleForState:UIControlStateNormal];//获取一级玩法名称
   
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.bounds.size);
    }];
    
}

//二级玩法，选择
- (void)actionForSubBetWayBtn:(CYButton *)sender{
    [self actionForBtn:sender tag:300];
    _secondIndex = sender.tag - 300;//获取二级玩法坐标
    self.secondTitle = [sender titleForState:UIControlStateNormal];//获取二级玩法名称
    if (_firstTitle  && _secondTitle){
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.superview).with.offset(-self.bounds.size.height);
        }];
        [UIView animateWithDuration:0.3 animations:^{
            self.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            _myblock([NSString stringWithFormat:@"%@-%@",_firstTitle, _secondTitle], _firstIndex, _secondIndex);
        }];
    }
}

//一级玩法按钮，二级玩法按钮，状态改变
- (void)actionForBtn:(CYButton *)sender tag:(NSUInteger)tag{
    sender.selected = YES;
    sender.layer.borderColor = [UIColor clearColor].CGColor;
    for (CYView *view in [self subviews]) {
        if ([view isKindOfClass:[CYButton class]] && view.tag >= tag) {
            CYButton *button = (CYButton *)view;
            if (button.tag != sender.tag) {
                button.selected = NO;
                button.layer.borderColor = ColorHex(0x888888).CGColor;
            }
        }
    }
    
}
- (void)show{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0.0, self.bounds.size.height + NAVIBAR_HEIGHT + STATUS_HEIGHT);
    }];
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
